<template>
  <a-modal
    :open="visible"
    title="修改房态"
    width="400px"
    @ok="handleOk"
    @cancel="handleCancel"
    @update:open="(val) => emit('update:visible', val)"
  >
    <a-form :model="formData" :rules="rules" ref="formRef" layout="vertical">
      <a-form-item label="房间号" name="roomNumber">
        <a-input v-model:value="formData.roomNumber" disabled />
      </a-form-item>

      <a-form-item label="当前状态" name="currentStatus">
        <a-input v-model:value="formData.currentStatus" disabled />
      </a-form-item>

      <a-form-item label="新状态" name="newStatus">
        <a-select v-model:value="formData.newStatus" placeholder="请选择新状态">
          <a-select-option value="vacant">空闲</a-select-option>
          <a-select-option value="occupied">占用</a-select-option>
          <a-select-option value="disabled">禁用</a-select-option>
          <a-select-option value="dirty">脏房</a-select-option>
          <a-select-option value="willDepart">将离</a-select-option>
          <a-select-option value="hourly">钟点房</a-select-option>
          <a-select-option value="earlyMorning">凌晨房</a-select-option>
        </a-select>
      </a-form-item>

      <a-form-item label="修改原因" name="reason">
        <a-textarea
          v-model:value="formData.reason"
          placeholder="请输入修改原因"
          :rows="3"
        />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { message } from 'ant-design-vue';
import type { FormInstance } from 'ant-design-vue';

interface Room {
  id: string;
  number: string;
  type: string;
  status: string;
  capacity: number;
  occupied: number;
}

interface Props {
  visible: boolean;
  room?: Room | null;
}

interface Emits {
  (e: 'update:visible', value: boolean): void;
}

const props = defineProps<Props>();
const emit = defineEmits<Emits>();

const formRef = ref<FormInstance>();
const formData = ref({
  roomNumber: '',
  currentStatus: '',
  newStatus: '',
  reason: '',
});

const statusLabels: Record<string, string> = {
  vacant: '空闲',
  occupied: '占用',
  disabled: '禁用',
  dirty: '脏房',
  willDepart: '将离',
  hourly: '钟点房',
  earlyMorning: '凌晨房',
};

const rules = {
  newStatus: [{ required: true, message: '请选择新状态' }],
};

// 监听房间变化
watch(
  () => props.room,
  (room) => {
    if (room) {
      formData.value = {
        roomNumber: room.number,
        currentStatus: statusLabels[room.status] || room.status,
        newStatus: '',
        reason: '',
      };
    }
  },
  { immediate: true },
);

const handleOk = async () => {
  try {
    await formRef.value?.validate();

    // 这里调用修改房态API
    message.success('房态修改成功');
    emit('update:visible', false);
  } catch (error) {
    console.error('表单验证失败:', error);
  }
};

const handleCancel = () => {
  emit('update:visible', false);
};
</script>
